<template>
  <div :style="{height: height + 'px'}">
    <editor v-model="myValue" :init="init" :disabled="disabled" :id="tinymceId"></editor>
  </div>
</template>
<script setup name="x-editor">
import {reactive, ref, onMounted, watch} from 'vue'

import tinymce from 'tinymce/tinymce'
import 'tinymce/skins/content/default/content.css'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/silver'
import 'tinymce/themes/silver/theme'
import 'tinymce/icons/default'
import 'tinymce/models/dom'
import "tinymce/icons/default"; // 引用图标文件
import "tinymce/plugins/link";
import "tinymce/plugins/code";
import "tinymce/plugins/table";
import "tinymce/plugins/lists";
import "tinymce/plugins/advlist";
import "tinymce/plugins/anchor";
import "tinymce/plugins/autolink"; //锚点
import "tinymce/plugins/autoresize";
import "tinymce/plugins/autosave";
import "tinymce/plugins/charmap"; //特殊字符
import "tinymce/plugins/code"; //查看源码
import "tinymce/plugins/codesample"; //插入代码
import "tinymce/plugins/directionality"; //
// import "tinymce/plugins/fullpage"; //页面属性编辑
import "tinymce/plugins/fullscreen"; //全屏
import "tinymce/plugins/help"; //帮助
// import "/tinymce/plugins/hr"; //横线
import "tinymce/plugins/image"; //图片
// import "tinymce/plugins/imagetools"; //图片工具
import "tinymce/plugins/importcss"; //图片工具
import "tinymce/plugins/insertdatetime"; //时间插入
import "tinymce/plugins/media"; //媒体插入
import "tinymce/plugins/nonbreaking"; //
// import "tinymce/plugins/noneditable"; //不间断空格
import "tinymce/plugins/pagebreak"; //分页
// import "tinymce/plugins/paste"; //粘贴
import "tinymce/plugins/preview"; //预览
// import "tinymce/plugins/print"; //打印
import "tinymce/plugins/quickbars"; //快捷菜单
import "tinymce/plugins/save"; //保存
import "tinymce/plugins/searchreplace"; //查询替换
// import "tinymce/plugins/spellchecker"; //拼写检查
// import "tinymce/plugins/tabfocus"; //
import "tinymce/plugins/template"; //插入模板
// import "tinymce/plugins/textpattern"; //
// import "tinymce/plugins/toc"; //
import "tinymce/plugins/visualblocks"; //
import "tinymce/plugins/visualchars"; //
import "tinymce/plugins/wordcount"; //数字统计


const emits = defineEmits(['update:modelValue', "change"])
const props = defineProps({
  modelValue: {
    type: String,
    default: "",
  },
  baseUrl: {
    type: String,
    default: "",
  },
  height: {
    type: Number,
    default: 400
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  plugins: {
    type: [String, Array],
    default: "wordcount visualchars visualblocks   template   searchreplace save quickbars  preview  pagebreak  nonbreaking media insertdatetime importcss  image   fullscreen  directionality codesample code charmap link code table lists advlist anchor autolink autoresize autosave",
  },//必填
  toolbar: {
    type: [String, Array],
    default:
        "formats undo redo paste print fontsizeselect fontselect template fullpage|wordcount ltr rtl visualchars visualblocks toc spellchecker searchreplace|save preview pagebreak nonbreaking|media image|outdent indent aligncenter alignleft alignright alignjustify lineheight  underline quicklink h2 h3 blockquote numlist bullist table removeformat forecolor backcolor bold italic  strikethrough hr charmap link insertdatetime|subscript superscript cut codesample code |anchor preview fullscreen|help",
  },//必填
})
//用于接收外部传递进来的富文本
const myValue = ref(props.modelValue)
const tinymceId = ref("vue-tinymce-" + +new Date() + ((Math.random() * 1000).toFixed(0) + ""))
//定义一个对象 init初始化
const init = reactive({
  selector: '#' + tinymceId.value, //富文本编辑器的id,
  language_url: "/tinymce/langs/zh-Hans.js", // 语言包的路径，具体路径看自己的项目，文档后面附上中文js文件
  language: "zh-Hans", //语言
  skin_url: "/tinymce/skins/ui/oxide", // skin路径，具体路径看自己的项目
  branding: true, //是否禁用“Powered by TinyMCE”
  menubar: true, //顶部菜单栏显示
  image_dimensions: false, //去除宽高属性
  plugins: props.plugins,  //这里的数据是在props里面就定义好了的
  toolbar: props.toolbar, //这里的数据是在props里面就定义好了的
  font_formats: 'Arial=arial,helvetica,sans-serif; 宋体=SimSun; 微软雅黑=Microsoft Yahei; Impact=impact,chicago;', //字体
  fontsize_formats: '11px 12px 14px 16px 18px 24px 36px 48px 64px 72px', //文字大小
// paste_convert_word_fake_lists: false, // 插入word文档需要该属性
  paste_webkit_styles: "all",
  paste_merge_formats: true,
  nonbreaking_force_tab: false,
  paste_auto_cleanup_on_paste: false,
  file_picker_types: 'file',
  content_css: '/tinymce/skins/content/default/content.css', //以css文件方式自定义可编辑区域的css样式，css文件需自己创建并引入
  //图片上传
  images_upload_handler: (blobInfo, progress) => new Promise((resolve, reject) => {

    if (blobInfo.blob().size / 1024 / 1024 > 2) {
      reject({message: '上传失败，图片大小请控制在 2M 以内', remove: true})
      return
    } else {


      Http.upload(blobInfo.blob(), {}).then((res) => {
        resolve(res.data.fileUrl);
      })
    }
  }),
// 文件上传
  file_picker_callback: (callback, value, meta) => {
    // Provide file and text for the link dialog
    if (meta.filetype == 'file') {
      callback('mypage.html', {text: 'My text'});
    }

    // Provide image and alt text for the image dialog
    if (meta.filetype == 'image') {
      callback('myimage.jpg', {alt: 'My alt text'});
    }

    // Provide alternative source and posted for the media dialog
    if (meta.filetype == 'media') {
      callback('movie.mp4', {source2: 'alt.ogg', poster: 'image.jpg'});
    }
  }
})
//监听外部传递进来的的数据变化
watch(() => props.modelValue, (value) => {
      myValue.value = value
      emits("change", myValue.value)
    },{immediate: true, deep: true}
)
//监听富文本中的数据变化
watch(
    () => myValue.value,
    () => {
      emits("change", myValue.value)
      emits('update:modelValue', myValue.value);
    },{immediate: true, deep: true}
)
//在onMounted中初始化编辑器
onMounted(() => {
  tinymce.init({})
})
</script>
<style>
.tox-promotion {
  display: none;
}

.tox-tinymce{
  height: 100% !important;
}
</style>
